package com.ck.ips.controller;

import com.ck.common.model.OperationResult;
import com.ck.common.model.Sysconfig;
import com.ck.core.annotation.LoginRequired;
import com.ck.core.annotation.SysLog;
import com.ck.core.constant.BusinessConstant;
import com.ck.core.constant.ModuleConstant;
import com.ck.core.constant.SysConfigNameEnum;
import com.ck.core.context.UserContextHolder;
import com.ck.ips.model.vo.FileGroupVO;
import com.ck.ips.service.file.FileGroupService;
import com.ck.ips.service.sys.SysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @description: 文件分组controller
 * @author: zhanglei
 * @date: 2019/4/30 14:46
 */
@LoginRequired
@Controller
@RequestMapping(value = "/fileGroup")
public class FileGroupController {

    @Autowired
    private FileGroupService fileGroupService;
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 添加文件分组
     * @param groupName 文件分组名称
     * @return
     */
    @ResponseBody
    @PostMapping("/addFileGroup")
    public Object addFileGroup(String groupName){
        //登录人id
        Long loginUserId = UserContextHolder.currentUser().getUserId();
        //当前时间
        Long nowTime = System.currentTimeMillis();
        if(StringUtils.isBlank(groupName)){
            return OperationResult.newValidateFailOperationResult("分组名称不能为空");
        }
        if(groupName.length()> BusinessConstant.FILE_GROUP_MAX_LENGTH){
            return OperationResult.newValidateFailOperationResult("分组名称长度不能超过"+BusinessConstant.FILE_GROUP_MAX_LENGTH+"个字符");
        }

        //获取当前文件分组总数
        int fileGroupTotal = fileGroupService.getFileGroupTotal();
        //获取配置文件中，文件分组最大限制
        Sysconfig sysConfigVO = sysConfigService.getByType(SysConfigNameEnum.FILE_GROUP_QUANTITY);
        Integer maxNumber = Integer.parseInt(sysConfigVO.getValue());
        if(fileGroupTotal >= maxNumber){
            return OperationResult.newValidateFailOperationResult("已经达到了分组的最大值，不能继续添加分组");
        }
        //检测该分组名称是否已经被使用过
        boolean isExist = fileGroupService.checkGroupNameExist(groupName);
        if(isExist){
            return OperationResult.newValidateFailOperationResult("该分组名称已经被使用过，请重新输入");
        }
        //将分组信息插入数据库
        return fileGroupService.addFileGroup(groupName,loginUserId,nowTime);
    }

    /**
     * 编辑分组
     * @param fileGroupId 原分组id
     * @param fileGroupName 新分组名称
     * @return
     */
    @ResponseBody
    @PostMapping("/editFileGroup")
    public Object editFileGroup(Long fileGroupId,String fileGroupName){
        //登录人id
        Long loginUserId = UserContextHolder.currentUser().getUserId();

        //当前时间
        Long nowTime = System.currentTimeMillis();

        if(fileGroupId==null){
            return OperationResult.newValidateFailOperationResult("原分组id不能为空");
        }
        if(StringUtils.isBlank(fileGroupName)){
            return OperationResult.newValidateFailOperationResult("分组名称不能为空");
        }
        if(fileGroupName.length()> BusinessConstant.FILE_GROUP_MAX_LENGTH){
            return OperationResult.newValidateFailOperationResult("分组名称长度不能超过"+BusinessConstant.FILE_GROUP_MAX_LENGTH+"个字符");
        }
        //检测该分组名称是否已经被使用过
        boolean isExist = fileGroupService.outSelfGroupNameExist(fileGroupName,fileGroupId);
        if(isExist){
            return OperationResult.newValidateFailOperationResult("该分组名称已经被使用过，请重新输入");
        }
        //修改分组信息
        return fileGroupService.editFileGroup(fileGroupId,fileGroupName,loginUserId,nowTime);
    }

    /**
     * 批量删除分组
     * @param fileGroupIds 分组id 英文逗号分隔
     * @return
     */
    @SysLog(module = ModuleConstant.MODULE_MATERIAL)
    @ResponseBody
    @PostMapping("/deletebyFileGroupIds")
    public Object deletebyFileGroupIds(String fileGroupIds){
        //登录用户的id
        Long loginUserId = UserContextHolder.currentUser().getUserId();

        if (StringUtils.isBlank(fileGroupIds)) {
            return OperationResult.newValidateFailOperationResult("分组id不能为空");
        }
        //删除分组
        return fileGroupService.deletebyFileGroupIds(loginUserId,fileGroupIds);
    }

    /**
     * 获取文件分组
     * @param fileType 文件类型
     * @return
     */
    @ResponseBody
    @PostMapping("/getFileGroup")
    public Object getFileGroup(String fileType){
        //获取所有的文件分组
        List<FileGroupVO> fileGroupVOList = fileGroupService.getNotApiFileGroup();
        //循环获取数量
        fileGroupService.addFileGroupVOTotal(fileGroupVOList,fileType);
        return fileGroupVOList;
    }

}
